package com.afinal.intelligencetrash.utils;

import android.content.Context;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.Map;
import java.util.Random;
import java.util.Set;

/**
 * Created by cyy on 17-10-8.
 */

public class SimpleUtil {
    private SimpleUtil(){}

    public static void showToast(Context context, String msg){
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    /*public static int generateUniqueRequestCode(String evidence){
        int sum = 0;
        for (int i = 0; i < evidence.length(); i++){
            sum += evidence.charAt(i);
        }
        sum %= 100;
        return evidence.length()+ sum;
    }*/

    public static Gson getGsonInstance(){
        return GsonHolder.instance;
    }

    private static class GsonHolder{
        public static Gson instance = new Gson();
    }

    public static int generateRadomNum(int from, int to){
        Random random = new Random(System.currentTimeMillis()%10000);
        return from + random.nextInt(to - from);
    }

    public static String generateJsonFromMap(Map<String, String> map){
        StringBuilder builder = new StringBuilder("{");
        int size = map.entrySet().size();
        int i = 0;
        for (Map.Entry<String, String> temp : map.entrySet()){
            String key = temp.getKey();
            String value = temp.getValue();
            builder.append("\"");
            builder.append(key);
            builder.append("\":\"");
            builder.append(value);
            builder.append("\"");
            if(i != size -1){
                builder.append(",");
            }
            i++;
        }
        builder.append("}");
        return builder.toString();
    }
}
